package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return permissionService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            return permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(String id) {
        return new Result(true, "数据回显成功", permissionService.findById(id));
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {
        try {
            return permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    @RequestMapping("/delete")
    public Result delete(String id) {
        return permissionService.delete(id);
    }

    //编辑把查询权限回显
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true, MessageConstant.find_quanxian_OK, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.find_quanxian_NOOK);
        }
    }
}
